<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\JasminumGallery\Controllers;

use GotchaDatabase\GotchaSettingsQuery;

class PhotosController {
    public function Render() {
        // Set proper header
        header ('Content-Type: image/jpeg');

        // Retrieve default gallery dir
        $settings = GotchaSettingsQuery::create()
                        ->filterByStrongName('GalleryMainDir')
                            ->findOne();

        // Build photo src
        $src = GOTCHA_ROOT_DIR.$settings->getValue().$_GET['src'];

        // List dimensions
        list($width, $height) = getimagesize($src);

        // Resizing
        $ratio = $width / $height;

        if ($ratio > 1) {
            $new_width = $_GET['width'];
            $new_height = $_GET['width'] / $ratio;
        } else {
            $new_width = $_GET['width'];
            $new_height =  $_GET['width'] * $ratio;
        }

        // Create a copy of a file
        $photo_copy = imagecreatefromjpeg($src);

        // Create template
        $photo_template = imagecreatetruecolor($width, $height);

        // Copy the original onto the template
        imagecopyresized($photo_template, $photo_copy, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        // Display photo
        ImageJpeg($photo_template);
    }
}